package com.login.sotoken.handler;

import cn.dev33.satoken.annotation.handler.SaAnnotationHandlerInterface;
import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.exception.SaTokenException;
import com.login.sotoken.annotation.CheckAccount;
import org.springframework.stereotype.Component;

import java.lang.reflect.AnnotatedElement;

/**
 * @Description：注解 CheckAccount 的处理器
 * @Author xiaohuang
 * @Date 2025/8/15
 */
@Component
public class CheckAccountHandler implements SaAnnotationHandlerInterface<CheckAccount> {
    @Override
    public Class<CheckAccount> getHandlerAnnotationClass() {
        return CheckAccount.class;
    }

    @Override
    public void checkMethod(CheckAccount checkAccount, AnnotatedElement element) {
        // 获取前端请求提交的参数
        String username = SaHolder.getRequest().getParamNotNull("username");
        String password = SaHolder.getRequest().getParamNotNull("password");
        //预注解值比较
        if(username.equals(checkAccount.username()) && password.equals(checkAccount.password()) ) {
            // 校验通过，什么也不做
        } else {
            // 校验不通过，则抛出异常
            throw new SaTokenException("账号或密码错误，未通过校验");
        }

    }
}
